package com.gomore.opple.module.main.good.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.gomore.opple.R;
import com.gomore.opple.rest.shopcart.SaveShoppingCartRequest;
import com.gomore.opple.service.AddtoShoppingCarListener;
import com.gomore.opple.service.SelectAllClickListener;
import com.gomore.opple.utils.BigDecimalUtils;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.web.cgform.goods.entity.TOGoodsEntity;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;
import com.gomore.opple.widgets.dialog.ConfirmDialog;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by asus on 2017/5/11.
 */
public class GoodAdapter extends CommonAdapter<TOGoodsEntity> {
    Activity activity;
    Context mContext;
    TOGoodsEntity toGoodsEntity;
    int width, height;
    private AddtoShoppingCarListener addtoShoppingCarListener;

    public void setAddtoShoppingCarListener(AddtoShoppingCarListener addtoShoppingCarListener) {
        this.addtoShoppingCarListener = addtoShoppingCarListener;
    }

    public GoodAdapter(Activity activity, Context context, int layoutId, List<TOGoodsEntity> toGoodsEntityList) {
        super(context, layoutId, toGoodsEntityList);
        mContext = context;
        this.activity = activity;
        int screenWidth = DensityUtil.screenWith(mContext);
        width = (screenWidth - DensityUtil.dip2px(mContext, 10 + 12 + 12)) / 2;
        height = (width * 13) / 17;
    }

    @Override
    protected void convert(ViewHolder holder, final TOGoodsEntity toGoodsEntity1, final int position) {
        final ImageView imageView = holder.getView(R.id.good_icon);
        ImageView add = holder.getView(R.id.add);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
        imageView.setLayoutParams(layoutParams);
        String url = "demo";
        if (toGoodsEntity1.getPictures() != null) {
            String[] urls = toGoodsEntity1.getPictures().split(",");
            url = urls[0];
        }
        Glide.with(mContext)
                .load(url)
                .asBitmap()
                .centerCrop()
                .override(width, height)
                .placeholder(R.mipmap.goods_place)
                .into(imageView);
        if (toGoodsEntity1.getName() != null) {
            holder.setText(R.id.name, toGoodsEntity1.getName());
        }
        if (toGoodsEntity1.getCode() != null) {
            holder.setText(R.id.code, toGoodsEntity1.getCode());
        }
        if (toGoodsEntity1.getSpec() != null) {
            holder.setText(R.id.type, "(" + toGoodsEntity1.getSpec() + ")");
        }
        if (toGoodsEntity1.getTagPrice() != null) {
            //保留两位小数
            BigDecimal str = BigDecimalUtils.forMate(toGoodsEntity1.getTagPrice());
            holder.setText(R.id.price, "¥" + str.toString());
        }

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addtoShoppingCarListener.addToShoppingCar(toGoodsEntity1.getId(), imageView);
            }
        });
    }
}
